import 'package:flutter/material.dart';

class QiZi extends CustomPainter {
  QiZi({
    Key? key,
    required this.offset,
    required this.width,
    required this.xian,
  }) : super();

  late final List<Offset> offset;
  final double width;
  final int xian;

  @override
  void paint(Canvas canvas, Size size) {
    var rect = Offset.zero & size;
    //画棋子
    // drawPieces(canvas, rect);
    drawPieces1(canvas, offset);
  }

  void drawPieces1(Canvas canvas, List<Offset> offsets) {
    //画一个黑子
    var paint = Paint()
      ..style = PaintingStyle.fill
      ..color = Colors.black;
    for (var i = 0; i < offsets.length; i++) {
      //画一个黑子
      paint.color = Colors.black;
      if (i % 2 == 0) {
        //画一个黑子
        canvas.drawCircle(
          offsets[i],
          width / xian / 2.8,
          paint,
        );
      } else {
        //画一个白子
        paint.color = Colors.white;
        canvas.drawCircle(
          offsets[i],
          width / xian / 2.8,
          paint,
        );
      }
    }
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    // TODO: implement shouldRepaint
    return true;
  }
}

class QiPan extends CustomPainter {
  QiPan({
    Key? key,
    required this.xian,
  }) : super();

  final int xian;
  @override
  void paint(Canvas canvas, Size size) {
    // TODO: implement paint
    var rect = Offset.zero & size;
    drawChessboard(canvas, rect);
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    // TODO: implement shouldRepaint
    return false;
  }

  void drawChessboard(Canvas canvas, Rect rect) {
    //棋盘背景
    var paint = Paint()
      ..isAntiAlias = true
      ..style = PaintingStyle.fill //填充
      ..color = Color(0xFFDCC48C);
    canvas.drawRect(rect, paint);

    //画棋盘网格
    paint
      ..style = PaintingStyle.stroke //线
      ..color = Colors.black38
      ..strokeWidth = 1.0;

    //画横线
    for (int i = 0; i <= xian; ++i) {
      double dy = rect.top + rect.height / xian * i;
      canvas.drawLine(Offset(rect.left, dy), Offset(rect.right, dy), paint);
    }

    for (int i = 0; i <= xian; ++i) {
      double dx = rect.left + rect.width / xian * i;
      canvas.drawLine(Offset(dx, rect.top), Offset(dx, rect.bottom), paint);
    }
  }
}
